var api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var app = getApp();
var MapContext = wx.createMapContext('map'); //当前地图的实例 

Page({
  data: {
    _height: 0,
    _longitude: 116.3972282409668, //当前地图位置的中心点(默认定位北京)
    _latitude: 39.90960456049752, //当前地图位置的中心点(默认定位北京)
    _scale: 10, //当前地图的缩放系数
    storeName:'', //搜索出来的门店名称
    markers: [], //覆盖物 
    searchShopList:[], //搜索出来的门店列表
    isClickSearch:false, //是否点击了搜索
    circles:[]
  },
  onLoad: function(options) {
    this.setData({
      _height: app.data._height
    });

    
  },
  onShow: function() {
    util.authUserLocation().then((res) => { //必须开启权限  
      wx.getLocation({
        success: (res) => {
          this.setData({
            _longitude: res.longitude,
            _latitude: res.latitude,
            _scale: 14
          });
        }
      })

      api.$get(api.API_GETSTORELST, {}).then((res) => {//添加地图覆盖物
        var markers = this.data.markers;
        res.forEach((item, index) => { //添加覆盖物
          markers.push({
            iconPath: "../../assets/others.png",
            id: item.id,
            longitude: item.storeCoord.split(',')[0],
            latitude: item.storeCoord.split(',')[1],
            width: 15,
            height: 15,
          })
        })
        this.setData({
          markers
        })

      })
    })
  }, 
  seeDetailHandle(e){ //查看具体某个门店地址 
    var item = e.currentTarget.dataset.item
    this.setData({
      _latitude: item.latitude,
      _longitude: item.longitude,
      storeName:'',
      searchShopList:[],
      isClickSearch:false
    })
  },

  markertap(e) { //点击覆盖物触发的事件
    wx.navigateTo({ //跳转到具体某个门店
      url: '../shopDetais/shopDetais?id=' + e.markerId,
    });
  },

  input_storeName(e){ //获取搜索的内容 
    var storeName = e.detail.value.trim();  
    this.setData({
      storeName: storeName,
      searchShopList: []
    });
    if (storeName==''){
      return
    }
    this._seachHandle() //搜索门店
  },

  _seachHandle(){//搜索门店名称  
    if (this.data.storeName == '') {
      wx.showToast({
        title: '请输入门店名称',
        icon: 'none'
      })
      return
    } 
    api.$get(api.API_GETSTORELST, { storeName: this.data.storeName}).then((res) => {//搜索门店名称弹出列表 
      var searchShopList = this.data.searchShopList;
      if (res.length==0){
        wx.showToast({
          title: '没有搜到相关信息',
          icon: 'none'
        })
        return
      }
 
      res.forEach((item, index) => { 
        searchShopList.push({
          storeName: item.storeName,
          storeAddr: item.storeAddr,
          id: item.id,
          longitude: item.storeCoord.split(',')[0],
          latitude: item.storeCoord.split(',')[1],

        })
      }) 
      
      this.setData({
        searchShopList
      })

    })
  },

  checkToInputHandle(){ //切换搜索模式
    this.setData({
      isClickSearch:true
    })
  },
  cancelSeachHandle(){ //取消搜索
    this.setData({
      storeName: '',
      searchShopList: [],
      isClickSearch: false
    })
  }
})